package com.dongge.service;

import com.dongge.dao.CURDDaoBase;
import com.dongge.entity.Article;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/10/17.
 */
@Service
public class ArticleService extends CURDDaoBase<Article>{

    public Page getCurrentPage(int targetPage,int sizePage){
        if(targetPage < 1){
            targetPage = 1;
        }
        String hsql = "from Article";
        String chsql = "select count(*) from Article";
        //count(*)返回的数据是long类型
        int count = ((Number)getSession().createQuery(chsql).uniqueResult()).intValue();
        List<Article> articles =  getSession().createQuery(hsql)
                .setFirstResult((targetPage-1)*sizePage)
                .setMaxResults(sizePage)
                .list();
        return  new Page(articles,targetPage,sizePage,count);
    }

    public EasyUIPage getEasyUIPage(int page,int rows){
        if(page < 1){
            page = 1;
        }
        String hsql = "from Article";
        String chsql = "select count(*) from Article";
        int count = ((Number)getSession().createQuery(chsql).uniqueResult()).intValue();
        List<Article> articles = getSession().createQuery(hsql)
                .setFirstResult((page-1)*rows)
                .setMaxResults(rows)
                .list();
        return new EasyUIPage(count,articles);
    }
}
